package com.winit.dataimport;

import com.winit.WebClientApplication;
import com.winit.commons.util.NumberUtil;
import com.winit.commons.util.ObjectToString;
import com.winit.commons.util.SeqGenerate;
import com.winit.feature.service.IFeatureService;
import com.winit.menu.model.MatrixMenu;
import com.winit.menu.repository.MatrixMenuOperations;
import com.winit.menu.repository.MatrixMenuRepository;
import com.winit.page.model.Page;
import com.winit.page.repository.PageRepository;
import com.winit.sellerportal.model.WinitMenu;
import com.winit.sellerportal.repository.WinitMenuRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = WebClientApplication.class)
public class SellMenu {

    @Autowired
    private WinitMenuRepository repository;
    @Autowired
    MatrixMenuRepository menuRepository;
    @Autowired
    MatrixMenuOperations menuOperations;
    @Autowired
    PageRepository pageRepository;
    @Autowired
    private IFeatureService featureService;
    private List<MatrixMenu> menuList = new ArrayList<>();
    private List<Page> pageList = new ArrayList<>();

    @Test
    public void Test1() throws Exception {
        MatrixMenu rootMenu = getRootMenu();
        List<String> rootChild = new ArrayList<>();
        getChild(0, rootChild, rootMenu);
        rootMenu.setChildren(rootChild);
        saveMenu(rootMenu);

        //保存menu和page
        menuRepository.save(menuList);
        for (Page page : pageList) {
            pageRepository.save(page);
            featureService.createPrototypeForPage(page);
        }
    }

    private MatrixMenu getRootMenu() {
        MatrixMenu menu = new MatrixMenu();
        menu.setId(SeqGenerate.createId("MENU"));
        menu.setMenu_name("SELLERPORTAL");
        menu.setReq_id("-1");
        menu.setSys("SELLERPORTAL");
        menu.setLevel("0");
        menu.setIs_del("1");
        menu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", "SELLERPORTAL");
        menu_data.put("routeId", " ");
        menu.setMenu_data(menu_data);
        menu.setMenuNum("ROOT-SELLERPORTAL");
        menu.setParent_id("0");
        return menu;
    }

    public void getChild(Integer parentId, List<String> child, MatrixMenu mMenu) throws Exception {
        List<WinitMenu> list = repository.findAllParent(parentId);
        if(null == list || list.size() == 0){
            //最下级菜单添加一个同名的page
            Page page = getPage(mMenu.getId(), mMenu.getMenu_name(), mMenu.getMenuNum());
            child.add(page.getId());
            savePage(page);
            return;
        }


        for (int i = 0; i < list.size(); i++) {
            WinitMenu menu = list.get(i);
            MatrixMenu matrixMenu = winitToMatrix(menu, Integer.parseInt(mMenu.getLevel()), mMenu.getMenuNum(), i + 1, mMenu.getId());
            child.add(matrixMenu.getId());

            List<String> c = new ArrayList<>();
            getChild(menu.getId(), c, matrixMenu);
            matrixMenu.setChildren(c);
            saveMenu(matrixMenu);
        }
    }

    private MatrixMenu winitToMatrix(WinitMenu menu, Integer level, String parentNum, Integer index, String parentId) throws Exception {
        MatrixMenu matrixMenu = new MatrixMenu();
        matrixMenu.setId(SeqGenerate.createId("MENU"));
        matrixMenu.setMenu_name(menu.getMenu_name());
        matrixMenu.setReq_id("-1");
        matrixMenu.setSys("SELLERPORTAL");
        matrixMenu.setLevel(String.valueOf(level + 1));
        matrixMenu.setIs_del("1");
        matrixMenu.setStatus("ONLINE");
        Map<String, String> menu_data = new HashMap<>();

        menu_data.put("name", menu.getMenu_name());
        menu_data.put("routeId", " ");
        matrixMenu.setMenu_data(menu_data);
        matrixMenu.setMenuNum(NumberUtil.createNumber(NumberUtil.SysEnum.OPC, parentNum, NumberUtil.TypeEnum.MENU, index));
        matrixMenu.setParent_id(parentId);
        return matrixMenu;
    }

    private Page getPage(String menuId, String label, String parent) throws Exception {
        Page page = new Page();
        page.setId(SeqGenerate.createId("MP"));
        page.setFileName(NumberUtil.createNumber(NumberUtil.SysEnum.TOM, parent, NumberUtil.TypeEnum.PAGE, 1));
        page.setMenuId(menuId);
        page.setTitle(label);
        return page;
    }

    private void saveMenu(MatrixMenu menu) {
        System.out.println(ObjectToString.getString(menu));
        menuList.add(menu);
    }

    private void savePage(Page page) {
        System.out.println(ObjectToString.getString(page));
        pageList.add(page);
    }
}
